﻿function SplitCssClass(cssName) {
    var css = cssName.split(" ");
    css = $.grep(css, function(n, i) {
        return n && n != "";
    });
    var result = "";
    var reg = /grid_\d+/ig;
    for (var i = 0; i < css.length; i++) {
        if (reg.test(css[i])) {
            result = css[i];
        }
    }
    return result;
}
function BuildForm(CanvasId, cols, rows) {
    var html = [];
    var w = 12 / cols;
    for (var j = 0; j < rows; j++) {
        html.push("<div class='container_12 gridrow'>");
        for (var i = 0; i < cols; i++) {
            html.push("<div class='grid_" + w);
            if (i == 0)
                html.push(" alpha");
            else if (i == cols - 1)
                html.push(" omega");

            html.push(" gridcell'></div>");
        }
        html.push("</div>");
    }

    $("#" + CanvasId).html(html.join(""));
    $("#" + CanvasId + " .gridcell").click(function() {
        $("#" + CanvasId + " .gridcell").filter(".checked").removeClass("checked");
        $(this).addClass("checked");
    });
}
function MergerToRight(CanvasId) {
    var checked = $("#" + CanvasId + " .gridcell").filter(".checked");
    if (checked.size() > 0 && !checked.hasClass("colspan")) {
        var next = checked.next();
        if (next.size() > 0) {
            var havePrev = checked.prev().size() > 0;
            var checkedClass = checked.attr("class");
            var nextClass = next.attr("class");
            checkedClass = SplitCssClass(checkedClass);
            nextClass = SplitCssClass(nextClass);
            var newClass = "grid_" + (parseInt(checkedClass.split("_")[1]) + parseInt(nextClass.split("_")[1]));
            checked.remove();
            next.removeClass(nextClass).addClass(newClass).addClass("rowspan");
            if (!havePrev)
                next.addClass("alpha");
        }
    }
}
function MergerToDown(CanvasId) {
    var checked = $("#" + CanvasId + " .gridcell").filter(".checked");
    if (checked.size() > 0) {
        var curRow = checked.parent();
        if (!curRow.hasClass("colspan")) {
            var nextRow = curRow.next();
            var nextCells = nextRow.find(".gridcell");
            $(".gridcell", curRow).each(function (i, o) {
                var checkedClass = $(o).attr("class");
                checkedClass = checkedClass.replace("gridcell", "");
                o = $(o).wrap("<div class='colspan " + checkedClass + "'></div>");
                //暂时屏蔽之
                //因为如果高度硬写的话后面展示的话高度就不再容易控制.比如隐藏一个一个单元格,下面的一个单元格位置 不容易上移
                if ($(o).hasClass("checked")) {
                    o = o.css("height", o.height() * 2);
                }
                else if (!$(o).hasClass("rowspan")) {
                    o = o.after(nextCells.eq(i).addClass("alpha").removeClass("omega"));
                    //o = o.after(nextCells);
                }
                else {
                    var rowspanclass = SplitCssClass(checkedClass);
                    var rowspannumber = parseInt(rowspanclass.split("_")[1]);
                    var nextObjectClone = $("<div class='" + checkedClass.replace(/rowspan|colspan/,"") + "'></div>");
                    nextObjectClone.append(nextCells.eq(i).addClass("alpha").removeClass("omega"));
                    var cellspannumber = parseInt(SplitCssClass(nextCells.eq(i).attr("class")).split("_")[1]);
                    if (nextCells.eq(i).size() > 0) {
                        var nextIndex = i + 1;
                        var nextCellnext = nextCells.eq(nextIndex);
                        while (nextCellnext.size() > 0 && cellspannumber < rowspannumber) {
                            var nextCellnextspannum = SplitCssClass(nextCellnext.attr("class")).split("_")[1];
                            cellspannumber += nextCellnextspannum;
                            if (cellspannumber >= rowspannumber)
                                nextCellnext.addClass("omega");
                            nextObjectClone.append(nextCellnext);
                            nextCellnext = nextCells.eq(++nextIndex);
                        }
                    }
                    o.after(nextObjectClone);
                }
                o.addClass("alpha ").removeClass("omega");
            });
            nextRow.remove();
            curRow.removeClass("gridrow").addClass("mulitrow");
        }
        else {
            var next = checked.next();
            next.remove();
            checked = checked.css("height", checked.height() * 2);
        }
    }
}